//
//  Common+Extensions.swift
//  Health_Infomation
//
//  Created by 林帆 on 2017/8/19.
//  Copyright © 2017年 林帆. All rights reserved.
//

import UIKit

extension CGSize{
    func sizeFromText () {
        
    }
}

extension UISearchBar {
    convenience  init(placeholder: String , imageName: String) {
        self.init()
        let  barImageView = self.subviews.first?.subviews.first //改格式
        barImageView?.layer.borderColor = kRGBColorFromHex(rgbValue: 0xfafafa).cgColor;
        barImageView?.layer.borderWidth = 1;
        self.placeholder = placeholder
        self.setImage(UIImage(named: imageName), for: .search, state: .normal)
        self.barTintColor = #colorLiteral(red: 0.9509041878, green: 0.9509041878, blue: 0.9509041878, alpha: 1)
    }
}

extension UILabel {
    
    convenience init(ffText : String, fontSize : CGFloat = 15, textColor : UIColor = kRGBColorFromHex(rgbValue: 0x333333)) {
        self.init()
        self.text = ffText
        self.textColor = textColor
        self.font = UIFont.systemFont(ofSize: fontSize)
        self.numberOfLines = 0
    }

}

extension UIViewController  {
    class dynamic func getCurrentController() -> UIViewController? {
        
        guard let window = UIApplication.shared.windows.first else {
            return nil
        }
        var tempView: UIView?
        for subview in window.subviews.reversed() {
            if subview.classForCoder.description() == "UILayoutContainerView" {
                tempView = subview
                break
            }
        }
        if tempView == nil {
            tempView = window.subviews.last
        }
        var nextResponder = tempView?.next
        var next: Bool {
            return !(nextResponder is UIViewController) || nextResponder is UINavigationController || nextResponder is UITabBarController
        }
        while next{
            tempView = tempView?.subviews.first
            if tempView == nil {
                return nil
            }
            nextResponder = tempView!.next
        }
        return nextResponder as? UIViewController
    }

}
